/**
 * 
 */
package com.pengtu.web.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.SuperviseGroup;
import com.pengtu.entity.app.SupervisorGroup;
import com.pengtu.service.app.SuperviseManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * @author Administrator
 *
 */
@Result(name=BaseActionSupport.RELOAD , location="/app/supervise.action",type="redirect")
public class SuperviseAction extends BaseActionSupport<SuperviseGroup> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SuperviseManager superviseManager;
	
	private String id;
	private SuperviseGroup superviseGroup;
	private List<SuperviseGroup> superviseList ;
	private Page<SuperviseGroup> page = new Page<SuperviseGroup>(10);
	private List<SupervisorGroup> supervisorList;
	
	@Override
	public SuperviseGroup getModel(){
		// TODO Auto-generated method stub
		return superviseGroup;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		if(!page.isOrderBySetted()){
			page.order(Page.ASC);
			page.orderBy("createDate");
		}
		page = superviseManager.findPageSe(page , queryFilter);
		return SUCCESS;
	}
	/**
	 * 树形展示监督组
	 * @return
	 * @throws Exception
	 */
	@Action("/account/json/supervise")
	public String searchForTree() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<SuperviseGroup> superviseGroups = superviseManager.findAllSe();
		for (SuperviseGroup supervise : superviseGroups) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("leaf", Boolean.TRUE);
			map.put("text", supervise.getName());
			map.put("id", supervise.getId());
			list.add(map);
		}
		Struts2Utils.renderJson(list, "no-cache");
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id)){
			Page<SupervisorGroup> sorPage = new Page<SupervisorGroup>(20);
			QueryFilter  sorQueryFilter  = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			sorQueryFilter.addFilter("superviseGroup.id",id);
			sorPage = superviseManager.findPageSor(sorPage, sorQueryFilter);
			if(null!=sorPage){
				supervisorList = sorPage.getResult();
				System.out.println(supervisorList.size());
			}
				
		}
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		superviseManager.saveSe(superviseGroup);
		String msg = "新增";
		if(StringUtils.isNotEmpty(id)){
			msg = "修改";
		}
		addActionMessage(msg+"监督组");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		superviseManager.deleteSe(id);
		addActionMessage("删除监督组信息!");
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id)){
			superviseGroup = superviseManager.findBySeProperty("id",id);
		}else{
			superviseGroup = new SuperviseGroup();
		}
	}
	
	@Autowired
	public void setSuperviseManager(SuperviseManager superviseManager) {
		this.superviseManager = superviseManager;
	}

	public Page<SuperviseGroup> getPage() {
		return page;
	}

	public void setPage(Page<SuperviseGroup> page) {
		this.page = page;
	}

	public List<SuperviseGroup> getSuperviseList() {
		return superviseList;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<SupervisorGroup> getSupervisorList() {
		return supervisorList;
	}


}
